package com.gm.commonlib.util.viewUtils;


import android.util.LruCache;

public class RepeatClickUtils {

    /**
     * 最小点击间隔 毫秒
     */
    public static final int MIN_CLICK_INTERVAL = 500;
    public static final int DOCUMENT_MIN_CLICK_INTERVAL = 1500;
    public static long lastClick = 0;

    //todo 最大检测数量 为 5，防止对象一直不回收
    public final static int MAX_CHECK_COUNT = 5;
    private final static LruCache<Object, ClickObject> checkTable = new LruCache<Object, ClickObject>(MAX_CHECK_COUNT){
        @Override
        protected ClickObject create(Object key) {
            return new ClickObject(0);
        }
    };

    public static boolean check() {
        long clickTime = System.currentTimeMillis();
        long time = clickTime - lastClick;
        if (time > 0 && time <= MIN_CLICK_INTERVAL) {
            return true;
        } else {
            lastClick = clickTime;
            return false;
        }
    }

    /**
     * 检查频率
     * @param clickInterval 频率识别 阈值
     * @return true 快速点击
     */
    public static boolean check(long clickInterval) {
        long clickTime = System.currentTimeMillis();
        long time = clickTime - lastClick;
        if (time > 0 && time <= clickInterval) {
            return true;
        } else {
            lastClick = clickTime;
            return false;
        }
    }

    public static boolean check(Object object) {
        return check(object, 1000, 3);
    }

    /**
     * todo 检测一个对象，在一段时间 clickInterval 内，是否有重复操作，
     * todo 如果在检测区间，有重复操作，累计次数，
     * todo 如果累计次数大于最大允许点击数，重新累计（达到一个按钮，不停的点，长时间、永远都可以检测到）
     * @param object 检测关联对象
     * @param clickInterval 防重复时间间隔
     * @param maxTipCount 防重时间间隔最大允许次数
     * @return 检测防重复结果 true : 重复点击， false 其他
     */
    public static boolean check(Object object, long clickInterval, int maxTipCount) {
        if (object == null) {
            return true;
        }
        synchronized (checkTable) {
            long clickTime = System.currentTimeMillis();
            ClickObject lastClickTime = checkTable.get(object);
            long time = clickTime - lastClickTime.getLastClickTime();
            if (time > 0 && time <= clickInterval) {
                //todo 发现了高频点击 操作
                if (lastClickTime.getClickTimes() >= maxTipCount) {
                    //todo 如果在该区间连续敲了5次，重新从开始时间计数,达到无限检测
                    lastClickTime.setClickTimes(0);
                    lastClickTime.setLastClickTime(clickTime);
                    checkTable.put(object, lastClickTime);
                } else {
                    lastClickTime.incClickTimes();
                    checkTable.put(object, lastClickTime);
                }
                return true;
            } else {
                lastClickTime.setClickTimes(0);
                lastClickTime.setLastClickTime(clickTime);
                checkTable.put(object, lastClickTime);
                return false;
            }
        }
    }

    public static void cleanCache() {
        checkTable.evictAll();
    }

    static class ClickObject {
        private int clickTimes;
        private long lastClickTime;

        public ClickObject(long lastClickTime) {
            clickTimes = 0;
            this.lastClickTime = lastClickTime;
        }

        public void setClickTimes(int clickTimes) {
            this.clickTimes = clickTimes;
        }

        public int getClickTimes() {
            return clickTimes;
        }

        public void incClickTimes() {
            clickTimes++;
        }

        public long getLastClickTime() {
            return lastClickTime;
        }

        public void setLastClickTime(long lastClickTime) {
            this.lastClickTime = lastClickTime;
        }
    }
}
